/*
    Dominate 3D - A cube-based game of life

    Copyright (C) 2007, Craig H. Miller (kidmosey@gmail.com)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __UTIL_H__
#define __UTIL_H__

#include <winsock2.h>
#include <string>
#include <stdio.h>

#define each(Iterator, IterStart, IterEnd) \
(__typeof__(IterStart) Iterator = IterStart; \
Iterator != IterEnd; \
Iterator++)

#define COUNT(x) (sizeof(x) / sizeof(*x))

extern int CheckReadStatus(SOCKET sockfd);
extern std::string ftoa(float f);
extern std::string itoa(int i);
extern char *GetLastErrorStr(char *szErr);
extern FILE *OpenBinaryFile(const char *filename);
extern char *CopyBinaryFile(const char *filename);

int filesize(const char *fname);
std::string GetFileContents(const std::string &fname);


template<typename StringContainer>
void SplitString(StringContainer &result, const std::string &str, const std::string &delim)
{
    size_t startPos = 0;
    while ((startPos = str.find_first_not_of(delim, startPos)) != std::string::npos)
    {
        size_t endPos = str.find_first_of(delim, startPos);
        if (endPos != std::string::npos)
            result.push_back(str.substr(startPos, endPos-startPos));
        else
            result.push_back(str.substr(startPos));

        startPos = endPos;
    }
}

template<class T>
void DeleteObject(T *obj)
{
    delete obj;
    obj = 0;
    return;
}

#endif // __UTIL_H__
